package com.example.administrator.daydayappmarket.recommend.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendMainThirdActivity;
import com.example.administrator.daydayappmarket.recommend.moduls.Details;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/12/29.
 */

public class RecommendListItem3 extends BaseAdapter {
    private List<Details.DataBean.RecommendAppsBean> data;
    private Context context;

    public RecommendListItem3(List<Details.DataBean.RecommendAppsBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Details.DataBean.RecommendAppsBean getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        DecimalFormat df = new DecimalFormat("#.##");
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend_main_third_3, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Details.DataBean.RecommendAppsBean item = getItem(position);
        String name = item.getName();
        final String id = item.getId();
        String icon_url = item.getIcon_url();
        String download_url = item.getDownload_url();
        String package_size = item.getPackage_size();
        double v = Double.parseDouble(package_size);
        String format = df.format(v);
        if(icon_url.startsWith("http")){
            Glide.with(context).load(icon_url).into(viewHolder.imageview);
        }else {
            Glide.with(context).load(icon_url).into(viewHolder.imageview);
        }
        viewHolder.name.setText(name);
        viewHolder.size.setText(format+"MB");
        viewHolder.search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, RecommendMainThirdActivity.class);
                intent.putExtra("id",id);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.item_recommend_main_third_3_imageview)
        ImageView imageview;
        @InjectView(R.id.item_recommend_main_third_3_name)
        TextView name;
        @InjectView(R.id.item_recommend_main_third_3_size)
        TextView size;
        @InjectView(R.id.item_recommend_main_third_3_search)
        LinearLayout search;
        @InjectView(R.id.item_recommend_main_third_3_download)
        RelativeLayout download;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
